﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UI.StorageManagement
{
    public partial class FrmStoTran : UI.ParentForm.BaseMain
    {
        FrmStoTranMedit edit = null;
        ToolStripButton tspSendButton = null;
        ToolStripButton tspRecButton = null;
        ToolStripButton tspModifyButton = null;
        ToolStripButton tspPrintPreButton = null;
        ToolStripButton trpTranPrintButton = null;

        public FrmStoTran(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
        }

        protected override void AddToolButton()
        {
            int firstBtnIndex = this.tspTop.Items.IndexOf(tspDel);

            this.tspTop.Items.Insert(firstBtnIndex, base.CreateToolBtn("W", "调拨单明细查询", new EventHandler(OnQueryBtnClick)));

            tspModifyButton = base.CreateToolBtn("J", "收货调整", new EventHandler(OnModifyBtnClick));
            this.tspTop.Items.Insert(firstBtnIndex, tspModifyButton);

            tspRecButton = base.CreateToolBtn("R", "收货", new EventHandler(OnRecBtnClick));

            this.tspTop.Items.Insert(firstBtnIndex, tspRecButton);

            trpTranPrintButton = base.CreateToolBtn("Q", "重印调拨单", new EventHandler(OnPrintDocBtnClick));
            this.tspTop.Items.Insert(firstBtnIndex, trpTranPrintButton);

            tspSendButton = base.CreateToolBtn("K", "发货", new EventHandler(OnSendBtnClick));
            this.tspTop.Items.Insert(firstBtnIndex, tspSendButton);

            tspPrintPreButton = base.CreateToolBtn("P", "打印配货单", new EventHandler(OnPrintBtnClick));
            //将打印配货但按钮加入在发货按钮之前
            this.tspTop.Items.Insert(firstBtnIndex, tspPrintPreButton);
        }

        private void OnPrintBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            FrmTranPreDoc frm = new FrmTranPreDoc();
            frm.AddParam("listid", Components.Validation.ToString(row.Cells["tranid"].Value));
            frm.ShowDialog();
            if (frm.IsReportOk) base.BindGrid();
        }

        private void OnPrintDocBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            FrmTranPrintDoc frm = new FrmTranPrintDoc();
            frm.AddParam("listid", Components.Validation.ToString(row.Cells["tranid"].Value));
            frm.ShowDialog();
        }

        private void OnQueryBtnClick(object sender, EventArgs e)
        {
            FrmStoTranQuery query = new FrmStoTranQuery(this.ModuleCode, "W");
            query.ShowDialog();
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("V", btn.Text, Components.Validation.ToString(row.Cells["tranid"].Value));
        }

        private void OnSendBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;
            OpenEdit("K", "发货", Components.Validation.ToString(row.Cells["tranid"].Value));
        }

        private void OnModifyBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;
            OpenEdit("J", "收货调整", Components.Validation.ToString(row.Cells["tranid"].Value));
        }

        private void OnRecBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;
            OpenEdit("R", "收货", Components.Validation.ToString(row.Cells["tranid"].Value));
        }

        protected override void OnEditClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("M", btn.Text, Components.Validation.ToString(row.Cells["tranid"].Value));
        }

        private void OpenEdit(string action, string btnText, string code)
        {
            edit = new FrmStoTranMedit(this.ModuleCode, action, GetTitleStyle(this.Text, btnText), code);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void OnAddClick(ToolStripButton btn)
        {
            edit = new FrmStoTranMedit(this.ModuleCode, "A", GetTitleStyle(this.Text, btn.Text), null);

            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.usercode";
        }

        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex == -1) return;

            string audstatus = Components.Validation.ToString(this.dgvMain["status", rowIndex].Value);

            tspEdit.Enabled = false;
            if (null != tspSendButton) tspSendButton.Enabled = false;
            if (null != tspRecButton) tspRecButton.Enabled = false;
            if (null != tspModifyButton) tspModifyButton.Enabled = false;
            if (null != tspPrintPreButton) tspPrintPreButton.Enabled = false;
            if (null != trpTranPrintButton) trpTranPrintButton.Enabled = false;

            if ("11" == audstatus) //已经打印了配货单，可以发货
            {
                if (null != tspSendButton) tspSendButton.Enabled = true;
            }
            else if ("1" == audstatus) //新增的需要打印配货单才能发货
            {
                tspEdit.Enabled = true;
                if (null != tspPrintPreButton) tspPrintPreButton.Enabled = true;
            }
            else if ("4" == audstatus) //发货
            {
                if (null != trpTranPrintButton) trpTranPrintButton.Enabled = true;
                if (null != tspRecButton) tspRecButton.Enabled = true;
            }
            else if ("10" == audstatus) //收货调整
            {
                if (null != tspModifyButton) tspModifyButton.Enabled = true;
            }
        }
    }
}
